import React, { PureComponent,Fragment } from "react";
import { connect } from "dva";
import moment from "moment";
import { Row, Col, Table, Spin, Card } from "antd";

@connect(({ point,global,loading }) => ({
  dictionaries:global.dictionaries,
  tableData: point.log.deviceList,
  loading: loading.models.point,
}))
export default class DeviceInfo extends PureComponent {
  constructor(props) {
    super(props);
    this.detailInfoId = this.props.match.params.id && this.props.match.params.id!='detail' ? this.props.match.params.id : 0;
    this.state={
      currentPage: 1,
      pageSize: 10,
    }
  }

  componentDidMount() {
    const {currentPage,pageSize} = this.state;
    this.getDataList(pageSize,currentPage)
  }

  getDataList(pageSize,currentPage) {
    const { dispatch } = this.props;
    dispatch({
      type: 'point/fetch_device_info',
      payload: {
        pointId: this.detailInfoId,
        pageSize,
        currentPage
      },
    });
  }

  // 分页、排序、筛选变化时触发
  handleTableChange = (pagination, filtersArg, sorter) => {
    const { pageSize } = this.state;

    this.setState({
      currentPage:pagination.current
    },()=>{
      this.getDataList(pageSize,pagination.current);
    })

  };


  render() {
    const { tableData,loading,dictionaries } = this.props;
    
    const deviceStatus = dictionaries.deviceStatus && dictionaries.deviceStatus.length>0
      ? dictionaries.deviceStatus.map(item=>item.label)
      :[];
      console.log(deviceStatus);
    const columns = [{
      title: '序号',
      dataIndex: 'index',
      render: (text, row, index)=> (
        <span>{index+1}</span>
      ),
    }, {
      title: '设备名称',
      dataIndex: 'devName'
    },{
    title: '设备编号',
    dataIndex: 'devCode',
    }, {
      title: '状态',
      dataIndex: 'status',
      render: (text)=> (
        <span>{deviceStatus[parseInt(text)-1]}</span>
      )
    }, {
      title: '保养次数',
      dataIndex: 'maintainCount'
    }, {
      title: '维修次数',
      dataIndex: 'repairCount'
    }//,{
    //   title: '设备名称',
    //   dataIndex: 'devName',
    // },
  ];

    return (
      <Card bodyStyle={{padding:0}} >
        <Table
          columns={columns}
          dataSource={tableData && tableData.list ? tableData.list : []}
          pagination={tableData && tableData.pagination}
          onChange={this.handleTableChange}
          loading={loading}
          rowKey={"taskId"}
        />
      </Card>

    );
  }
}
